﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.DAL.IDAL.HnxInfo;
using StockTrader.BusinessService.DataContract.HnxInfo;
using MySql.Data.MySqlClient;
using System.Data;
using StockTrader.DAL.MySQLHelper;

namespace StockTrader.DAL.MySQL.HnxInfo
{
    public class HnxMarket : IHnxMarket
    {
        private MySqlConnection _internalConnection;
        private MySqlTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new MySqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters

        private const string PARM_DATE_NO = "@DATENO";
        private const string PARM_TRADING_DATE = "@TRADINGDATE";
        private const string PARM_CURRENT_STATUS = "@STATUS";
        private const string PARM_TIME = "@TIME";
        private const string PARM_TOTAL_TRADE = "@TRADE";
        private const string PARM_TOTAL_STOCK = "@STOCK";
        private const string PARM_TOTAL_QTTY = "@QTTY";
        private const string PARM_TOTAL_VALUE = "@VALUE";
        private const string PARM_ADVANCES = "@ADVANCE";
        private const string PARM_NOCHANGE = "@NOCHANGE";
        private const string PARM_DECLINES = "@DECLINE";
        private const string PARM_CHG_INDEX = "@CHG";
        private const string PARM_PCT_INDEX = "@PCT";
        private const string PARM_MARKET_INDEX = "@MARKET";
        private const string PARM_PT_TOTAL_QTTY = "@PTQTTY";
        private const string PARM_PT_TOTAL_VALUE = "@PTVALUE";
        private const string PARM_PT_TOTAL_TRADE = "@PTTRADE";
        private const string PARM_BASE_INDEX = "@BASE";
        private const string PARM_HIGH = "@HIGH";
        private const string PARM_LOW = "@LOW";
        private const string PARM_DATENO = "@DATENO";
        private const string PARM_PT = "@PT";
        private const string PARM_PT_VALUE = "@PTVALUE";
        private const string PARM_PT_TRADE = "@PTTRADE";
        private const string PARM_TIMESTAMP = "@TIMESTAMP";

        #endregion

        #region SQL

        private const string INSERT_HNX_MARKETINFO = "INSERT INTO HNX_MARKETINFO(TRADINGDATE, TIME, STATUS, TRADE, STOCK, QTTY, VALUE, ADVANCE, NOCHANGE, DECLINE, CHG, PCT, MARKET, BASE, HIGH, LOW, DATENO, PT, PTVALUE, PTTRADE, TIMESTAMP) VALUES (@TRADINGDATE, @TIME, @STATUS, @TRADE, @STOCK, @QTTY, @VALUE, @ADVANCE, @NOCHANGE, @DECLINE, @CHG, @PCT, @MARKET, @BASE, @HIGH, @LOW, @DATENO, @PT, @PTVALUE, @PTTRADE, @TIMESTAMP) ON DUPLICATE KEY UPDATE TRADINGDATE=@TRADINGDATE, TIME=@TIME, STATUS=@STATUS, TRADE=@TRADE, STOCK=@STOCK, QTTY=@QTTY, VALUE=@VALUE, ADVANCE=@ADVANCE, NOCHANGE=@NOCHANGE, DECLINE=@DECLINE, CHG=@CHG, PCT=@PCT, MARKET=@MARKET, BASE=@BASE, HIGH=@HIGH, LOW=@LOW, DATENO=@DATENO, PT=@PT, PTVALUE=@PTVALUE, PTTRADE=@PTTRADE, TIMESTAMP=@TIMESTAMP";


        #endregion


        #region IHnxMarket Members


        public List<HnxMarketData> getHnxNewMarketInfo(decimal timeStamp)
        {
            throw new NotImplementedException();
        }

        public List<HnxMarketData> getHnxAllMarketInfo()
        {
            throw new NotImplementedException();
        }

        public List<HnxMarketData> countHnxMarketInfo()
        {
            throw new NotImplementedException();
        }

        public void deleteHnxMarketInfo()
        {
            throw new NotImplementedException();
        }

        public void insertHnxNewMarketInfo(HnxMarketData market)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                    new MySqlParameter( PARM_TRADING_DATE,MySqlDbType.DateTime),
                    new MySqlParameter( PARM_TIME,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_CURRENT_STATUS,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_TOTAL_TRADE,MySqlDbType.Int64),
                    new MySqlParameter( PARM_TOTAL_STOCK,MySqlDbType.Int32),
                    new MySqlParameter( PARM_TOTAL_QTTY,MySqlDbType.Int64),
                    new MySqlParameter( PARM_TOTAL_VALUE,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_ADVANCES,MySqlDbType.Int32),
                    new MySqlParameter( PARM_NOCHANGE,MySqlDbType.Int32),
                    new MySqlParameter( PARM_DECLINES,MySqlDbType.Int32),
                    new MySqlParameter( PARM_CHG_INDEX,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_PCT_INDEX,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_MARKET_INDEX,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_BASE_INDEX,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_HIGH,MySqlDbType.Int64),
                    new MySqlParameter( PARM_LOW,MySqlDbType.Int64),
                    new MySqlParameter( PARM_DATENO,MySqlDbType.Int64),
                    new MySqlParameter( PARM_PT,MySqlDbType.Int64),
                    new MySqlParameter( PARM_PT_VALUE,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_PT_TRADE,MySqlDbType.Int64),
                    new MySqlParameter( PARM_TIMESTAMP,MySqlDbType.Int64)
                };

                parms[0].Value = market.TradingDate;
                parms[1].Value = market.Time;
                parms[2].Value = market.Status;
                parms[3].Value = market.Trade;
                parms[4].Value = market.Stock;
                parms[5].Value = market.Qtty;
                parms[6].Value = market.Value;
                parms[7].Value = market.Advance;
                parms[8].Value = market.Nochange;
                parms[9].Value = market.Decline;
                parms[10].Value = market.Chg;
                parms[11].Value = market.Pct;
                parms[12].Value = market.Market;
                parms[13].Value = market.BaseIndex;
                parms[14].Value = market.High;
                parms[15].Value = market.Low;
                parms[16].Value = market.DateNo;
                parms[17].Value = market.Pt;
                parms[18].Value = market.PtTrade;
                parms[19].Value = market.TimeStamp;

                MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_HNX_MARKETINFO, parms);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
